home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte14 / getchpl.c < prev    next >
C/C++ Source or Header  |  1996-01-31  |  8KB  |  232 lines

  1.  
  2. #include <windows.h>  
  3. #include "GetChPl.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. LPCSTR lpszText = "This is a sample text string";
  108. char   szNewText[29];
  109. int    anDX[28];
  110. USHORT auGlyphs[28];    
  111.  
  112.  
  113. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  114. {
  115.    switch( uMsg )
  116.    {
  117.       case WM_COMMAND :
  118.               switch( LOWORD( wParam ) )
  119.               {
  120.                  case IDM_TEST :
  121.                         {
  122.                            HDC         hDC;
  123.                            HFONT       hFont, hOldFont;
  124.                            RECT        rect;
  125.                            GCP_RESULTS results;
  126.  
  127.                            // Create font to use.
  128.                            //....................
  129.                            hFont = CreateFont( 16, 0, 0, 0, FW_NORMAL,
  130.                                                FALSE, FALSE, FALSE,
  131.                                                ANSI_CHARSET, 
  132.                                                OUT_DEFAULT_PRECIS,
  133.                                                CLIP_DEFAULT_PRECIS,
  134.                                                PROOF_QUALITY,
  135.                                                DEFAULT_PITCH | FF_DONTCARE,
  136.                                                "Times New Roman" );
  137.  
  138.  
  139.                            SetRect( &rect, 10, 10, 110, 30 );
  140.  
  141.                            // Retrieve device context and
  142.                            // select the font into it.
  143.                            //............................
  144.                            hDC = GetDC( hWnd );
  145.                            hOldFont = SelectObject( hDC, hFont );
  146.  
  147.                            Rectangle( hDC, rect.left-3, rect.top-3, 
  148.                                            rect.right+3, rect.bottom+3 );
  149.  
  150.                            // Set up the structure for calling
  151.                            // the GetCharacterPlacement() function.
  152.                            //......................................
  153.                            results.lStructSize = sizeof( results );
  154.                            results.lpOutString = &szNewText[0];
  155.                            results.lpOrder     = NULL;
  156.                            results.lpDx        = &anDX[0];
  157.                            results.lpCaretPos  = NULL;
  158.                            results.lpClass     = NULL;
  159.                            results.lpGlyphs    = &auGlyphs[0];
  160.                            results.nGlyphs     = 15;
  161.                            results.nMaxFit     = 0;
  162.                     
  163.                            // Justify the text to fit within the 
  164.                            // maximum extent defined by the rect.
  165.                            //....................................
  166.                            GetCharacterPlacement( hDC, lpszText, 15, 
  167.                                                   rect.right-rect.left,
  168.                                                   &results, 
  169.                                                   GCP_JUSTIFY|GCP_MAXEXTENT );
  170.  
  171.                            // Output the justified text.
  172.                            //...........................
  173.                            ExtTextOut( hDC, rect.left, rect.top, 0,
  174.                                        NULL, szNewText, 
  175.                                        strlen( szNewText ), anDX );
  176.  
  177.                            // Output the normal text.
  178.                            //........................
  179.                            ExtTextOut( hDC, 10, 40, 0, NULL, 
  180.                                        lpszText, strlen( lpszText ), NULL );
  181.                           
  182.                            SelectObject( hDC, hOldFont );
  183.                            ReleaseDC( hWnd, hDC );
  184.                            DeleteObject( hFont );
  185.                         }
  186.                         break;
  187.  
  188.                  case IDM_ABOUT :
  189.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  190.                         break;
  191.  
  192.                  case IDM_EXIT :
  193.                         DestroyWindow( hWnd );
  194.                         break;
  195.               }
  196.               break;
  197.       
  198.       case WM_DESTROY :
  199.               PostQuitMessage(0);
  200.               break;
  201.  
  202.       default :
  203.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  204.    }
  205.  
  206.    return( 0L );
  207. }
  208.  
  209.  
  210. LRESULT CALLBACK About( HWND hDlg,           
  211.                         UINT message,        
  212.                         WPARAM wParam,       
  213.                         LPARAM lParam)
  214. {
  215.    switch (message) 
  216.    {
  217.        case WM_INITDIALOG: 
  218.                return (TRUE);
  219.  
  220.        case WM_COMMAND:                              
  221.                if (   LOWORD(wParam) == IDOK         
  222.                    || LOWORD(wParam) == IDCANCEL)    
  223.                {
  224.                        EndDialog(hDlg, TRUE);        
  225.                        return (TRUE);
  226.                }
  227.                break;
  228.    }
  229.  
  230.    return (FALSE); 
  231. }
  232.